<?php

namespace common\repositories\expcenter;

use Yii;
use common\repositories\BaseRepository;
use common\entities\expcenter\ExpCenterEntity;

class ExpCenterRepository extends BaseRepository {

    /** 启用 */
    const STATUS_YES = 1;

    /** 禁用 */
    const STATUS_NO = 0;

    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 更新密码
     * @param int $uid
     * @param string $pwd
     * @return boolean
     */
    public function updatePwd($uid, $pwd) {
        $entity = ExpCenterEntity::find()->where(["uid" => $uid])->one();
        if (is_null($entity)) {
            return false;
        }
        $entity->pwd = $pwd;
        $entity->modified_on = time();
        $entity->modified_by = Yii::$app->user->identity->username;
        $entity->save();
        return true;
    }

    /**
     * 更新密码
     * @param int $uid
     * @param int $status
     * @return boolean
     */
    public function updateStatus($uid) {
        $entity = ExpCenterEntity::find()->where(["uid" => $uid])->one();
        if (is_null($entity)) {
            return false;
        }
        if ($entity->status == self::STATUS_YES) {
            $entity->status = self::STATUS_NO;
        } else {
            $entity->status = self::STATUS_YES;
        }
        $entity->modified_on = time();
        $entity->modified_by = Yii::$app->user->identity->username;
        $entity->save();
        return true;
    }

    /**
     * 创建用户
     * @param \common\models\expcenter\ExpCenterModel $model
     */
    public function addExpCenter($model) {
        $model->created_on = time();
        $model->modified_on = time();

        $entity = new ExpCenterEntity();
        $this->setFieldEntity($entity, $model);

        $result = $entity->save();
        if (false === $result) {
            return $this->setAndReturn(-1, implode(',', array_values($entity->getFirstErrors())));
        }
        return $entity->uid;
    }

    /**
     * 设置数据
     * @param \common\entities\expcenter\ExpCenterEntity $entity
     * @param \common\models\expcenter\ExpCenterModel $model
     */
    private function setFieldEntity(&$entity, $model) {
        $entity->created_on = $model->created_on;
        $entity->created_by = $model->created_by;
        $entity->modified_on = $model->modified_on;
        $entity->modified_by = $model->modified_by;
        $entity->username = $model->username;
        $entity->pwd = $model->pwd;
        $entity->status = $model->status;
    }

//    public function getArticleInfo ($aId) {
//        $query = ArticleEntity::find()
//                ->where(['a_id' => $aId])
//                ->asArray()
//                ->one();
//        $query['content'] = htmlspecialchars_decode($query['content']);
//        return $query;
//    }

    public function getExpCenterUsers($limit = 10) {
        $connection = ExpCenterEntity::getDb();
        $sql = "select * from " . ExpCenterEntity::tableName() . "
        order by created_by desc limit {$limit}";
        $command = $connection->createCommand($sql);
        $result = $command->queryAll();
        return $result;
    }

    public function getExpCenterUsersCount() {
        $count = ExpCenterEntity::find()->count();
        return $count;
    }

}
